package com.probiz.estore.product.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang.StringUtils;

import com.probiz.estore.common.model.product.ProductAttGroup;
import com.probiz.estore.common.model.product.ProductAttGroupItem;
import com.probiz.estore.common.model.product.ProductType;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.product.dao.ProductAttGroupDao;
import com.probiz.estore.product.dao.ProductAttGroupItemDao;
import com.probiz.estore.product.service.ProductAttGroupItemManager;


/**
 * Manager implementation for ProductAttGroupItem, responsible for business processing, and communicate between web and persistence layer.
 */
public class ProductAttGroupItemManagerImpl extends GenericManagerImpl<ProductAttGroupItem> implements ProductAttGroupItemManager {

	private ProductAttGroupItemDao productAttGroupItemDao = null;
	private ProductAttGroupDao productAttGroupDao = null;

	/**
	 * @param productAttGroupItemDao
	 *            the productAttGroupItemDao to set
	 */
	public void setProductAttGroupItemDao(ProductAttGroupItemDao productAttGroupItemDao) {
		this.productAttGroupItemDao = productAttGroupItemDao;
	}

	public void setProductAttGroupDao(ProductAttGroupDao productAttGroupDao) {
		this.productAttGroupDao = productAttGroupDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = productAttGroupItemDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ProductAttGroupItem entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ProductAttGroupItem entity) {

	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductAttGroupItemManager#getProductAttGroupItemByProductTypeAndAttribute(java.lang.Integer, java.lang.Integer)
	 */
	public ProductAttGroupItem getProductAttGroupItemByProductTypeAndAttribute(
			Integer productTypeId, Integer attributeId){
		return productAttGroupItemDao.getProductAttGroupItemByProductTypeAndAttribute(productTypeId,attributeId);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.product.service.ProductAttGroupItemManager#saveAttrItemsFromArrays(com.probiz.estore.common.model.product.ProductType, java.lang.String[], java.lang.String[], java.lang.String[], java.lang.String[], java.lang.String[], java.lang.String[], java.lang.String[])
	 */
	public List<String> saveAttrItemsFromArrays(
			ProductType productType, String[] attrItemGroupIds,
			String[] attrItemIds, String[] attrItemSortOrders,
			String[] attrItemIsQuickSearchs, String[] attrItemShowStyles,
			String[] attrItemSearchValues, String[] attrItemAttributeIds){
		List<String> newAttrItemIdList = new ArrayList<String>();
		//删除attrItem
		List<String> attrItemIdList=Arrays.asList(attrItemIds);
		Set<ProductAttGroupItem> formerAttrItemList=new HashSet<ProductAttGroupItem>();//原来的attrItem
		
		List<ProductAttGroup> productAttGroupList = productAttGroupDao.listByProperty("productType.productTypeId", productType.getProductTypeId());
		for(ProductAttGroup group : productAttGroupList){
			formerAttrItemList.addAll(group.getProductAttGroupItems());
		}
		for (ProductAttGroupItem attrItem : formerAttrItemList) {
			if(!attrItemIdList.contains(attrItem.getProductAttGroupItemId().toString())){
				deleteById(attrItem.getProductAttGroupItemId());
			}
		}
		//增加或者修改attrItem
		for (int i = 0; i < attrItemIds.length; i++) {
			ProductAttGroupItem attrItem;
			if(attrItemIds[i].equals("")){
				//case:新增
				attrItem = new ProductAttGroupItem();
			}else{
				//case:修改
				attrItem = getById(Integer.valueOf(attrItemIds[i]));
			}
			attrItem.setSortOrder(i);//界面上不需要用attrItemSortOrders
			if(StringUtils.isNotBlank(attrItemIsQuickSearchs[i])){
				attrItem.setIsQuickSearch(Integer.valueOf(attrItemIsQuickSearchs[i]));
			}
			if(StringUtils.isNotBlank(attrItemShowStyles[i])){
				attrItem.setShowStyle(Short.valueOf(attrItemShowStyles[i]));
			}
			attrItem.setSearchValue(attrItemSearchValues[i]);
			attrItem.setProductAttGroupId(Integer.valueOf(attrItemGroupIds[i]));
			attrItem.setAttributeId(Integer.valueOf(attrItemAttributeIds[i]));
			attrItem.setProductTypeId(productType.getProductTypeId());
			save(attrItem);
			newAttrItemIdList.add(attrItem.getProductAttGroupItemId().toString());
		}
		return newAttrItemIdList;
	}
	
	
	public Long getNumOfQuickSearchAttItemsByProductTypeId(Integer productTypeId){
		return productAttGroupItemDao.getNumOfQuickSearchAttItemsByProductTypeId(productTypeId);
	}

	
	public List<ProductAttGroupItem> getQuickSearchAttItemsByProductTypeId(Integer productTypeId){
		return productAttGroupItemDao.getQuickSearchAttItemsByProductTypeId(productTypeId);
	}

}
